package com.yt.product.server.api.client;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.product.models.service.IInsuranceProductService;
import com.yt.product.models.bo.InsuranceProductBO;
import com.yt.product.models.dto.InsuranceProductDTO;
import com.yt.product.models.entity.InsuranceProduct;
import com.yt.product.server.utils.InsuranceProductConverter;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 保险产品表控制器
 *
 * @author yuanxinwen
 * @Date 2025-10-30 16:12:52
 */

@RestController
@RequestMapping("/api/insuranceProduct")
@Slf4j
@Api(value = "InsuranceProductApiController",tags = "保险产品表Api接口")
public class InsuranceProductApiController {

    @DubboReference
    private IInsuranceProductService insuranceProductService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找保险产品详情", notes = "根据id查找保险产品详情")
    @ApiImplicitParam(name = "id", value = "保险产品ID", required = true, paramType = "query", dataType = "Long")
    public Result<InsuranceProductDTO> get(@RequestParam("id") Long id){
        try {
            // 参数校验
            if(id == null || id <= 0){
                return new Result<>(CodeEnum.FAIL_100002.getCode(), "保险产品ID不能为空且必须大于0");
            }

            // 查询保险产品详情
            InsuranceProduct entity = insuranceProductService.getById(id);
            
            // 检查产品是否存在
            if (entity == null) {
                return new Result<>(CodeEnum.FAIL_100002.getCode(), "未找到指定的保险产品");
            }
            
            // 检查产品是否已删除
            if (entity.getIsDelete() != null && entity.getIsDelete() == 1) {
                return new Result<>(CodeEnum.FAIL_100002.getCode(), "保险产品已被删除");
            }

            // 转换为DTO
            InsuranceProductDTO dto = InsuranceProductConverter.entityToDTO(entity);

            return new Result<>(dto);
        } catch (Exception e) {
            log.error("查询保险产品详情时发生异常: ", e);
            return new Result<>(CodeEnum.FAIL_100005.getCode(), "查询保险产品详情失败");
        }
    }

    @ApiOperation(value = "保险产品表列表",notes = "保险产品表列表")
    @PostMapping("/queryPageList")
    public Result<Page<InsuranceProductDTO>> queryPageList(InsuranceProductBO bo){
        try {
            QueryWrapper<InsuranceProduct> wrapper = new QueryWrapper<>();
            if (bo != null) {
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
                if (bo.getStartDate() != null){
                    wrapper.ge("create_time", bo.getStartDate());
                }
                if (bo.getEndDate() != null){
                    wrapper.le("create_time", bo.getEndDate());
                }
            }

            // 设置默认分页参数
            int pageNo = 1;
            int pageSize = 10;

            // 安全地获取分页参数
            if (bo != null) {
                Integer boPageNo = bo.getPageNo();
                Integer boPageSize = bo.getPageSize();
                
                if (boPageNo != null && boPageNo > 0) {
                    pageNo = boPageNo;
                }
                if (boPageSize != null && boPageSize > 0) {
                    pageSize = boPageSize;
                }
            }

            // 确保分页参数合法
            if (pageNo <= 0) pageNo = 1;
            if (pageSize <= 0) pageSize = 10;
            if (pageSize > 100) pageSize = 100; // 限制最大页面大小

            Page<InsuranceProduct> page = new Page<>(pageNo, pageSize);
            page = insuranceProductService.page(page, wrapper);
            
            // 转换Page<InsuranceProduct>为Page<InsuranceProductDTO>
            Page<InsuranceProductDTO> dtoPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
            // 使用stream转换并收集到新的列表中
            List<InsuranceProductDTO> dtoList = page.getRecords().stream()
                    .map(InsuranceProductConverter::entityToDTO)
                    .collect(Collectors.toList());
            dtoPage.setRecords(dtoList);

            return new Result<>(dtoPage);
        } catch (Exception e) {
            log.error("查询保险产品列表时发生异常: ", e);
            return new Result<>(CodeEnum.FAIL_100005);
        }
    }
}